<template>
  <div id="app-other">

    <div class="one-block-2">
      <a-space>
        <a-button @click="sendMsgStart" :disabled="started">开始</a-button>
        <a-button @click="sendMsgStop" :disabled="!started">结束</a-button>
      </a-space>
      <div style="margin:10px;">结果:{{cardResult.msg}}</div>
      <div style="border: solid 1px #ccc; line-height: 20px;padding: 5px; margin-bottom:10px;border-radius: 5px;width: 70%;min-height: 150px;">
      {{ messageString }}
      </div>
      <div v-if="cardInfo && cardInfo.id!=undefined">
        <div>居民身份证号：{{cardInfo.id}}</div>
        <div>姓名：{{cardInfo.name}}</div>
        <div>性别：{{cardInfo.gender}}</div>
        <div>民族码：{{cardInfo.nation}}</div>
        <div>出生：{{cardInfo.birthday}}</div>
        <div>住址：{{cardInfo.address}}</div>
        <div>签发机关：{{cardInfo.agency}}</div>
        <div>有效期起始日期：{{cardInfo.expireStart}}</div>
        <div>有效期截至日期：{{cardInfo.expireEnd}}</div>
      </div>
    </div>
  </div>
</template>
<script>

import {ipcApiRoute, specialIpcRoute} from "@/api/main";

export default {
  data() {
    return {
      messageString: '请将身份证放入读卡器感应区',
      cardResult: {},
      cardInfo: {},
      started:false,
    };
  },
  mounted () {
    this.init();
  },
  methods: {
    init () {
      const self = this;
      // 避免重复监听，或者将 on 功能写到一个统一的地方，只加载一次
      this.$ipc.removeAllListeners(ipcApiRoute.ipcSendIdCardMsg);
      this.$ipc.on(ipcApiRoute.ipcSendIdCardMsg, (event, result) => {
        console.log('[ipcRenderer] [socketMsgStart] result:', result);
        if(result==='停止了'){
          return;
        }

        self.messageString = result;
        self.cardResult = result;
        if(result && (Object.prototype.toString.call(result)=="[object Object]") && result.status==0){
          self.cardInfo = result.data;
        }else{
          self.cardInfo={};
        }

      })

      this.sendMsgStart();
    },
    sendMsgStart() {
      const params = {
        type: 'start',
        content: '开始'
      }
      this.$ipc.send(ipcApiRoute.ipcSendIdCardMsg, params);
      this.started = true;
    },
    sendMsgStop() {
      const params = {
        type: 'end',
        content: ''
      }
      this.$ipc.send(ipcApiRoute.ipcSendIdCardMsg, params)
      this.started = false;
    },
  }
};
</script>
<style lang="less" scoped>
#app-other {
  padding: 0px 10px;
  text-align: left;
  width: 100%;
  .one-block-1 {
    font-size: 16px;
    padding-top: 10px;
  }
  .one-block-2 {
    padding-top: 10px;
  }
}
</style>
